package com.quanyue.mechanics.complex.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.mechanics.complex.mapper.CaseMapper;
import com.quanyue.mechanics.complex.service.CaseService;
import com.quanyue.mechanics.domain.entity.Case;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CaseServiceImpl implements CaseService {
    @Autowired
    private CaseMapper caseMapper;

    @Override
    public Case addCase(Case ca) {
        ca.setDelFlag(false);
        ca.setCreateTime(LocalDateTime.now());
        ca.setUpdateTime(LocalDateTime.now());
        caseMapper.insert(ca);
        return ca;
    }

    @Override
    public void updateCase(Case ca) {
        Case check = caseMapper.findOne(ca.getId());
        if (check == null) throw new AssertUtils.ValidateException("error case check");
        ca.setCreateTime(check.getCreateTime());
        ca.setUpdateTime(LocalDateTime.now());
        ca.setDelFlag(false);
        caseMapper.updateSelective(ca);
    }

    @Override
    public void deleteCase(Integer id) {
        Case check = caseMapper.findOne(id);
        if (check == null) throw new AssertUtils.ValidateException("error case check");
        check.setDelFlag(true);
        caseMapper.updateSelective(check);
    }

    @Override
    public Page <Case> findCase(PageRequest request) {
        request.setOrderBy("create_time");
        request.setAsc(false);
        return caseMapper.findLimitValid(request);
    }
}
